<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="fragments/easyui_insdep_head :: easyui_insdep_head" th:with="title='库存管理'">
</head>
<link th:href="@{/filter/Fliter.css}" rel="stylesheet"/>
<script type="text/javascript" th:src="@{/filter/Filter.js}"/>

<body>

<table id="tt" class="easyui-datagrid" style="width:100%;height:100%"
       toolbar="#ttb" pageList='[1,10,20,30,50]' singleSelect="true"
       rownumbers="true" pagination="true">
    <thead>
    <tr>
        <th field="barcode" width="160">条形码</th>
        <th field="warename" width="500">名称</th>
        <th field="warehousename" width="100">仓库名</th>
        <th field="stocknum" width="50">库存数量</th>
    </tr>
    </thead>
    <div id="ttb" style="padding:3px">
        <div id="filter"></div>
        <span>仓库:</span>
        <input class="easyui-combobox"
              id="warehouseid"
               data-options="
					url:'/warehouseList',
					method:'post',
					valueField:'warehouseid',
					textField:'warehousename',
			" />
        <span>名称:</span>
        <input id="warename" style="line-height:26px;border:1px solid #ccc"/>
        <span>条形码:</span>
        <input id="barcode" style="line-height:26px;border:1px solid #ccc"/>
        <input id="componentid" type="hidden"/>
        <input id="componentparameterdetailid" type="hidden"/>
        <input type="checkbox" id="isFuzzy"/>模糊查询
        <a href="#" class="easyui-linkbutton" plain="true" onclick="doSearch()">查找</a>
        <a href="#" class="easyui-linkbutton" plain="true" onclick="doReSet()">重置</a>

        <script type="text/javascript">
            //<![CDATA[
            $(function () {
                //单选
                $('#filter').comboboxfilter({
                    url:'/component/getFilterComponent',
                    text: "配件",
                    onSelect: function (itemData) {
                        $("#componentid").val(itemData.id);
                        doSearch();
                        // 获取他的属性
                        $.post("/component/parameter/getComponentParameter", {
                            componentid: itemData.id
                        }, function (data) {
                            if (typeof (data) == typeof ("string")) {
                                data = $.parseJSON(data);
                            }
                            // 遍历属性
                            $.each(data, function (i, item) {
                                // 新建一个div
                                var id = "filter" + i;
                                $('#filter').after("<div id = '" + id + "' name='Dfilter'/ >")
                                // 为该div初始化值
                                id = '#' + id;
                                $(id).comboboxfilter({
                                    url: '/component/parameter/detail/getFilterComponentParameterDetail',
                                    text: item.componentparametername,
                                    param: {
                                        componentparameterid: item.componentparameterid
                                    },
                                    onSelect: function (itemData) {
                                        $("#componentparameterdetailid").val(itemData.id);
                                        doSearch();
                                    },
                                    unSelect: function (itemData) {
                                        $("#componentparameterdetailid").val("");
                                        doSearch();
                                    }
                                });
                            });
                        })
                    },
                    unSelect: function (itemData) {
                        $("#componentid").val("");
                        doSearch();
                        $("div[name='Dfilter']").each(function () {
                            $(this).comboboxfilter('clear');
                            $(this).remove();
                        });
                    }
                });

                $('#tt').datagrid({
                    url: "/stock/getStock",
                    onDblClickRow : function (index, row) {
                        $.messager.confirm('警告', '确定要直接修改库存吗', function (r) {
                            if (r) {
                                $.messager.prompt('库存数'+row.stocknum, '正在修改仓库('+row.warehousename+')的商品('+row.warename+')', function(r){
                                    if ((/(^[1-9]\d*$)/.test(r))){
                                        $.post("/stock/updateStock", {stockid: row.stockid, stocknum:r}, function (data) {
                                            var data = data == "success" ? "修改成功" : "修改失败";
                                            $.messager.alert('结果', data, 'info');
                                            $("#tt").datagrid("reload");
                                        });
                                    } else {
                                        $.messager.alert("警告", "请输入正整数");
                                    }
                                });
                            }
                        });
                    }
                });
            });

            function doSearch() {
                $('#tt').datagrid('load', {
                    warename: $('#warename').val(),
                    barcode: $('#barcode').val(),
                    warehouseid: $("#warehouseid").combobox("getValue"),
                    isFuzzy: $('#isFuzzy').is(':checked'),
                    componentid: $('#componentid').val(),
                    componentparameterdetailid: $('#componentparameterdetailid').val(),
                });
            }
            function doReSet() {
                $('#warename').val("");
                $('#barcode').val();
                $("#warehouseid").combobox("clear"),
                doSearch();
            }
            //]]>
        </script>
    </div>
</table>

</body>
</html>